<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      python/路飞项目/1-项目基础/2-虚拟环境的搭建 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>python/路飞项目/1-项目基础/2-虚拟环境的搭建</h2>



  <p class="post-date">2019-10-25</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h1 id="虚拟环境的搭建"><a href="#虚拟环境的搭建" class="headerlink" title="虚拟环境的搭建"></a>虚拟环境的搭建</h1><h2 id="优点"><a href="#优点" class="headerlink" title="优点"></a>优点</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">1、使不同应用开发环境相互独立</span><br><span class="line">2、环境升级不影响其他应用，也不会影响全局的python环境</span><br><span class="line">3、防止出现包管理混乱及包版本冲突</span><br></pre></td></tr></table></figure>



<h2 id="windows"><a href="#windows" class="headerlink" title="windows"></a>windows</h2><h5 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># 建议使用pip3安装到python3环境下</span><br><span class="line">pip3 install virtualenv</span><br><span class="line">pip3 install virtualenvwrapper-win</span><br></pre></td></tr></table></figure>

<h5 id="配置虚拟环境管理器工作目录"><a href="#配置虚拟环境管理器工作目录" class="headerlink" title="配置虚拟环境管理器工作目录"></a>配置虚拟环境管理器工作目录</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 配置环境变量：</span><br><span class="line"># 控制面板 &#x3D;&gt; 系统和安全 &#x3D;&gt; 系统 &#x3D;&gt; 高级系统设置 &#x3D;&gt; 环境变量 &#x3D;&gt; 系统变量 &#x3D;&gt; 点击新建 &#x3D;&gt; 填入变量名与值</span><br><span class="line">变量名：WORKON_HOME  变量值：自定义存放虚拟环境的绝对路径</span><br><span class="line">eg: WORKON_HOME: D:\Virtualenvs</span><br><span class="line"></span><br><span class="line"># 同步配置信息：</span><br><span class="line"># 去向Python3的安装目录 &#x3D;&gt; Scripts文件夹 &#x3D;&gt; virtualenvwrapper.bat &#x3D;&gt; 双击</span><br></pre></td></tr></table></figure>



<h2 id="MacOS、Linux"><a href="#MacOS、Linux" class="headerlink" title="MacOS、Linux"></a>MacOS、Linux</h2><h5 id="安装-1"><a href="#安装-1" class="headerlink" title="安装"></a>安装</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># 建议使用pip3安装到python3环境下</span><br><span class="line">pip3 install -i https:&#x2F;&#x2F;pypi.douban.com&#x2F;simple virtualenv</span><br><span class="line">pip3 install -i https:&#x2F;&#x2F;pypi.douban.com&#x2F;simple virtualenvwrapper</span><br></pre></td></tr></table></figure>

<h5 id="工作文件"><a href="#工作文件" class="headerlink" title="工作文件"></a>工作文件</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 先找到virtualenvwrapper的工作文件 virtualenvwrapper.sh，该文件可以刷新自定义配置，但需要找到它</span><br><span class="line"># MacOS可能存在的位置 &#x2F;Library&#x2F;Frameworks&#x2F;Python.framework&#x2F;Versions&#x2F;版本号文件夹&#x2F;bin</span><br><span class="line"># Linux可能所在的位置 &#x2F;usr&#x2F;local&#x2F;bin  |  ~&#x2F;.local&#x2F;bin  |  &#x2F;usr&#x2F;bin</span><br><span class="line"># 建议不管virtualenvwrapper.sh在哪个目录，保证在 &#x2F;usr&#x2F;local&#x2F;bin 目录下有一份</span><br><span class="line"># 如果不在 &#x2F;usr&#x2F;local&#x2F;bin 目录，如在 ~&#x2F;.local&#x2F;bin 目录，则复制一份到 &#x2F;usr&#x2F;local&#x2F;bin 目录</span><br><span class="line">	-- sudo cp -rf ~&#x2F;.local&#x2F;bin&#x2F;virtualenvwrapper.sh &#x2F;usr&#x2F;local&#x2F;bin</span><br></pre></td></tr></table></figure>

<h5 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 在 ~&#x2F;.bash_profile 完成配置，virtualenvwrapper的默认默认存放虚拟环境路径是 ~&#x2F;.virtualenvs</span><br><span class="line"># WORKON_HOME&#x3D;自定义存放虚拟环境的绝对路径，需要自定义就解注</span><br><span class="line">VIRTUALENVWRAPPER_PYTHON&#x3D;&#x2F;usr&#x2F;local&#x2F;bin&#x2F;python3</span><br><span class="line">source &#x2F;usr&#x2F;local&#x2F;bin&#x2F;virtualenvwrapper.sh</span><br><span class="line"></span><br><span class="line"># 在终端让配置生效：</span><br><span class="line">	-- source ~&#x2F;.bash_profile</span><br></pre></td></tr></table></figure>



<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"># 在终端工作的命令</span><br><span class="line"></span><br><span class="line"># 1、创建虚拟环境到配置的WORKON_HOME路径下</span><br><span class="line"># 选取默认Python环境创建虚拟环境：</span><br><span class="line">	-- mkvirtualenv 虚拟环境名称</span><br><span class="line"># 基于某Python环境创建虚拟环境：</span><br><span class="line">	-- mkvirtualenv -p python2.7 虚拟环境名称</span><br><span class="line">	-- mkvirtualenv -p python3.6 虚拟环境名称</span><br><span class="line"></span><br><span class="line"># 2、查看已有的虚拟环境</span><br><span class="line">	-- workon</span><br><span class="line"></span><br><span class="line"># 3、使用某个虚拟环境</span><br><span class="line">	-- workon 虚拟环境名称</span><br><span class="line">	</span><br><span class="line"># 4、进入|退出 该虚拟环境的Python环境</span><br><span class="line">	-- python | exit()</span><br><span class="line"></span><br><span class="line"># 5、为虚拟环境安装模块</span><br><span class="line">	-- pip或pip3 install 模块名</span><br><span class="line"></span><br><span class="line"># 6、退出当前虚拟环境</span><br><span class="line">	-- deactivate</span><br><span class="line"></span><br><span class="line"># 7、删除虚拟环境(删除当前虚拟环境要先退出)</span><br><span class="line">	-- rmvirtualenv 虚拟环境名称</span><br></pre></td></tr></table></figure>



<h2 id="pycharm使用"><a href="#pycharm使用" class="headerlink" title="pycharm使用"></a>pycharm使用</h2><h5 id="新建项目"><a href="#新建项目" class="headerlink" title="新建项目"></a>新建项目</h5><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ggqvohby3ij30lr0dlwfe.jpg" alt=""></p>
<h5 id="添加环境"><a href="#添加环境" class="headerlink" title="添加环境"></a>添加环境</h5><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ggqvotdttxj30nb0fzdgt.jpg" alt=""></p>
<h5 id="使用环境"><a href="#使用环境" class="headerlink" title="使用环境"></a>使用环境</h5><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ggqvoob9gbj30lr0dlab0.jpg" alt=""></p>
</section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/python/%E8%B7%AF%E9%A3%9E%E9%A1%B9%E7%9B%AE/1-%E9%A1%B9%E7%9B%AE%E5%9F%BA%E7%A1%80/1-pip%E6%BA%90/">
        <span class="nav-arrow">← </span>
        
          python/路飞项目/1-项目基础/1-pip源
        
      </a>
    
    
      <a class="nav-right" href="/python/MySQL%E5%85%A5%E9%97%A8/5-Mac%E5%AE%89%E8%A3%85MySQL/">
        
          python/MySQL入门/5-Mac安装MySQL
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#虚拟环境的搭建"><span class="toc-nav-text">虚拟环境的搭建</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#优点"><span class="toc-nav-text">优点</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#windows"><span class="toc-nav-text">windows</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-5"><a class="toc-nav-link" href="#安装"><span class="toc-nav-text">安装</span></a></li><li class="toc-nav-item toc-nav-level-5"><a class="toc-nav-link" href="#配置虚拟环境管理器工作目录"><span class="toc-nav-text">配置虚拟环境管理器工作目录</span></a></li></ol></li></ol></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#MacOS、Linux"><span class="toc-nav-text">MacOS、Linux</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-5"><a class="toc-nav-link" href="#安装-1"><span class="toc-nav-text">安装</span></a></li><li class="toc-nav-item toc-nav-level-5"><a class="toc-nav-link" href="#工作文件"><span class="toc-nav-text">工作文件</span></a></li><li class="toc-nav-item toc-nav-level-5"><a class="toc-nav-link" href="#配置"><span class="toc-nav-text">配置</span></a></li></ol></li></ol></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#使用"><span class="toc-nav-text">使用</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#pycharm使用"><span class="toc-nav-text">pycharm使用</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-5"><a class="toc-nav-link" href="#新建项目"><span class="toc-nav-text">新建项目</span></a></li><li class="toc-nav-item toc-nav-level-5"><a class="toc-nav-link" href="#添加环境"><span class="toc-nav-text">添加环境</span></a></li><li class="toc-nav-item toc-nav-level-5"><a class="toc-nav-link" href="#使用环境"><span class="toc-nav-text">使用环境</span></a></li></ol></li></ol></li></ol></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/python/路飞项目/1-项目基础/2-虚拟环境的搭建/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "python/路飞项目/1-项目基础/2-虚拟环境的搭建",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>